package org.linlinjava.litemall.wx.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import org.apache.commons.codec.binary.Base64;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.wx.entity.AccessToken;
import org.linlinjava.litemall.wx.vo.LineColor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/wx/qrcode")
public class WxQrCodeController {

    private static final String APP_ID = "wx5907808d0cc13657";
    private static final String APP_SECRET = "d5e6cc8a4ea439be33f706bdec2ee968";
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}";
    private static final String QR_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";

    @GetMapping("create")
    public Object createQrCode(@RequestParam String scene,
                               @RequestParam(defaultValue = "pages/index/index") String page,
                               @RequestParam(defaultValue = "true") boolean check_path,
                               @RequestParam(defaultValue = "release") String env_version,
                               @RequestParam(defaultValue = "430") int width,
                               @RequestParam(defaultValue = "false") boolean auto_color,
                               LineColor lineColor,
                               @RequestParam(defaultValue = "true") boolean is_hyaline) {

        // 获取微信AccessToken
        String accessToken = null;
        try {
            accessToken = queryAccessToken();
            RestTemplate template = new RestTemplate();

            Map<String, Object> map = new HashMap<>();
            map.put("scene", scene);
            map.put("page", page);
            map.put("check_path", check_path);
            map.put("env_version", env_version);
            map.put("width", width);
            map.put("auto_color", auto_color);
            if (Objects.isNull(lineColor)) {
                map.put("line_color", new LineColor(0, 0, 0));
            }
            map.put("line_color", lineColor);
            map.put("is_hyaline", is_hyaline);

            ResponseEntity<byte[]> responseEntity = template.postForEntity(QR_CODE_URL + accessToken, map, byte[].class);
            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                throw new Exception("生成二维码错误");
            }
            return ResponseUtil.ok(Base64.encodeBase64String(responseEntity.getBody()));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail();
        }

    }

    private String queryAccessToken() throws Exception {
        RestTemplate template = new RestTemplate();
        String queryRes = template.getForObject(ACCESS_TOKEN_URL, String.class, APP_ID, APP_SECRET);
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectReader reader = objectMapper.readerFor(AccessToken.class);
        AccessToken accessToken = reader.readValue(queryRes);
        return accessToken.getAccessToken();
    }

    public static void main(String[] args) {
        Object qrCode = new WxQrCodeController().createQrCode(null, null, true, null, 130, true, null, true);
        System.out.println(qrCode);
    }

}
